#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

# -*- mode: python; -*-
import platform
import subprocess

Import('BuildEnv')

env = BuildEnv.Clone()
env.CppDisableExceptions()
env.Append(CPPPATH = [env['TOP']])

SandeshGenFiles = env.SandeshGenCpp('io.sandesh')
SandeshGenSrcs = env.ExtractCpp(SandeshGenFiles)

except_env = BuildEnv.Clone()
except_env.Append(CPPPATH = env['TOP_INCLUDE'] + '/thrift')
except_env.Append(CPPPATH = [env['TOP']])
EventManagerSrc = except_env.Object('event_manager.cc')
SslServerSrc = except_env.Object('ssl_server.cc')

usock_env = BuildEnv.Clone()
usock_env.CppEnableExceptions()
usock_env.Append(CPPPATH = env['TOP'])
usock_server = usock_env.Object('usock_server.cc')

IoSrc = [
    'io_utils.cc',
    'ssl_session.cc',
    'tcp_message_write.cc',
    'tcp_server.cc',
    'tcp_session.cc',
    'udp_server.cc',
    'process_signal_common.cc',
]

IoSrc += [
    'nix/process_signal.cc',
    usock_server,
]

src_root = Dir('#').srcnode().abspath

gpp_version = subprocess.check_output(
    "g++ --version | grep g++ | awk '{print $3}'",
    shell=True).rstrip()
gpp_version_major = int(gpp_version.split(".")[0])

create_boost_ssl_dir =  env.Command("create_boost_ssl_dir", [],
                                Mkdir(src_root + '/build/include/boost/asio/ssl/'))
create_boost_ssl_impl_dir = env.Command("create_boost_ssl_impl_dir", [],
                                Mkdir(src_root + '/build/include/boost/asio/ssl/impl/'))
cp_boost_context =  env.Command("cp_boost_context", [],
                                Copy(src_root + '/build/include/boost/asio/ssl/impl/',
                                src_root + "/third_party/boost_1_53_tlsv12_fix/context.ipp"))
cp_boost_context_base =  env.Command("cp_boost_context_base", [],
                                Copy(src_root + '/build/include/boost/asio/ssl/',
                                src_root + "/third_party/boost_1_53_tlsv12_fix/context_base.hpp"))

env.Depends(cp_boost_context, create_boost_ssl_impl_dir)
env.Depends(cp_boost_context_base, create_boost_ssl_dir)
env.Depends(SslServerSrc, cp_boost_context)
env.Depends(SslServerSrc, cp_boost_context_base)

# IO
contrail_common_io_doc_files = []
io_doc_target = BuildEnv['TOP'] + '/io/'
contrail_common_io_doc_files += BuildEnv.SandeshGenDoc('#src/contrail-common/io/io.sandesh', io_doc_target)
env['IO_DOC_FILES'] = contrail_common_io_doc_files
Export('contrail_common_io_doc_files')

libio = env.Library('io',
            SandeshGenSrcs +
            EventManagerSrc +
            SslServerSrc +
            IoSrc)
env.Install(env['TOP_LIB'], libio)
env.SConscript('test/SConscript', exports='BuildEnv', duplicate = 0)
